package com.leyantech.ecp.agenx.task.filter;

import com.leyantech.ecp.agenx.session.client.SessionClient;
import com.leyantech.utility.Logger;

import com.google.inject.Inject;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nullable;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-15.
 */
public abstract class AssistantOnlineFilter<Task> implements TaskFilter<Task> {

  protected final Logger log = new Logger(getClass());

  @Inject
  protected SessionClient sessionClient;

  protected abstract String getStoreId(Task task);

  protected abstract String getAssistantId(Task task);

  @Nullable
  protected Object logMessage(Task task) {
    return null;
  }

  @Override
  public boolean test(Task task) {
    final String storeId = getStoreId(task);
    final String assistantId = getAssistantId(task);
    if (StringUtils.isAnyBlank(storeId, assistantId)) {
      log.info("store {} assistant {} must not be blank", storeId, assistantId);
      return false;
    }
    final boolean isAssistantOnline = sessionClient.isAssistantOnline(storeId, assistantId);
    if (!isAssistantOnline) {
      final Object logMessage = logMessage(task);
      final String message = logMessage == null
          ? "store {} assistant {} offline"
          : "store {} assistant {} offline, msg {}";
      log.info(message, storeId, assistantId, logMessage);
    }
    return isAssistantOnline;
  }

}
